package com.weiyuwqj.controller.sms;


import com.weiyuwqj.api.service.SysNoticeTypeService;
import com.weiyuwqj.api.service.SysTempcodeService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.SysNoticeType;
import com.weiyuwqj.api.model.SysTempcode;
//import com.weiyuwqj.api.service.SmsService;
import com.weiyuwqj.api.util.IpUtils;
import com.weiyuwqj.api.util.RandomCodesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/8/19.
 */
@Controller
public class SmsController {

    @Autowired
    SysNoticeTypeService noticeTypeService;

    @Autowired
    SysTempcodeService tempcodeService;

    @Autowired
    BasUserService userService;
/*
    @Autowired
    SmsService smsService;*/

    public Map smsInfo(String validPhoneCode,String mobile,SysNoticeType noticeType){
        String content = noticeType.getTypeContent();
        String  randomCode = validPhoneCode;
        System.out.println("您的验证码：" + randomCode);
        content = content.replace(Constants.VALIDATECODE, randomCode);

        //SmsService sms = new YiMeiSmsImpl();
        //smsService.sendSms(mobile,content,null,0 ,"" ).toString();
        Map ret = new HashMap();
        ret.put("result","success");
        ret.put("messageText","发送成功");
        return ret;
    }

    //注册短信
    @ResponseBody
    @RequestMapping(value="/registerSendSms")
    public Object registerSend(HttpServletRequest request){
        Date newDate=new Date();
        String pictureCode=request.getParameter("pictureCode");
        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }
        String validPhoneCode = RandomCodesUtils.createRandom(true,4);
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        String mobile=request.getParameter("mobile");
        BasUser user=userService.getUserByMobile(mobile);
        if(user!=null){
            throw new CrowdException("sys.user.add.user_exist");
        }
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("reg_validatecode_sms");
        return smsInfo(validPhoneCode,mobile,noticeType);
    }

    //微信注册短信
    @ResponseBody
    @RequestMapping(value="/weixinRegisterSendSms")
    public Object weixinRegisterSendSms(HttpServletRequest request){
        Date newDate=new Date();
        String pictureCode=request.getParameter("pictureCode");
        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }
        String validPhoneCode = RandomCodesUtils.createRandom(true,4);
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        String mobile=request.getParameter("mobile");

        BasUser user=userService.getUserByMobile(mobile);
        if(user!=null){
            throw new CrowdException("sys.user.add.user_exist");
        }
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("reg_validatecode_sms");
        return smsInfo(validPhoneCode,mobile,noticeType);
    }

    //登录短信
    @ResponseBody
    @RequestMapping(value="/doLoginSendSms")
    public Object doLoginSend(HttpServletRequest request){
        Date newDate=new Date();
        String pictureCode=request.getParameter("pictureCode");
        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        String mobile=request.getParameter("mobile");
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("login_sms");
        return smsInfo(validPhoneCode,mobile,noticeType);
    }

    //修改绑定旧手机短信
    @ResponseBody
    @RequestMapping(value="/user/oldModifyMobileSms")
    public Object oldModifyMobileSms(HttpServletRequest request){
        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        Integer id=(Integer)request.getSession().getAttribute("userId");
        BasUser user=userService.selectByKey(id);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("modifymobile_sms");
        return smsInfo(validPhoneCode,user.getMobile(),noticeType);
    }

    //修改绑定新手机短信
    @ResponseBody
    @RequestMapping(value="/user/newModifyMobileSms")
    public Object newModifyMobileSms(HttpServletRequest request,String inputCode){
        Long validPhoneExpiredTime = (Long) request.getSession().getAttribute("validPhoneExpiredTime");
        String validPhoneCode = request.getSession().getAttribute("validPhoneCode").toString();

        //发送短信
        Date now = new Date();
        long between = (now.getTime() - validPhoneExpiredTime) / 1000;

        if (!(validPhoneCode.equals(inputCode))) {
            throw new CrowdException("user.register.randomCode.error");
        }
        if (between > Constants.VALIDATETIME) {
            throw new CrowdException("user.register.randomCode.timeout");
        }

        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("vaildPhoneCode",validPhoneCode);
        String mobile=request.getParameter("mobile");
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("modifymobile_sms");
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        return smsInfo(validPhoneCode,mobile,noticeType);
    }


    //找回密码
    @ResponseBody
    @RequestMapping(value="/backPasswordSms")
    public Object backPasswordSms(HttpServletRequest request){
        String mobile=request.getParameter("mobile");
        BasUser user=userService.getUserByMobile(mobile);
        if(user==null){
            throw new CrowdException("user.login.username.no_such_user");
        }
        String pictureCode=request.getParameter("pictureCode");
        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }

        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);

        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("forgetpass_sms");
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        return smsInfo(validPhoneCode,mobile,noticeType);
    }

    //找回手势密码
    @ResponseBody
    @RequestMapping(value="/user/backPatternLockSms")
    public Object backPatternLockSms(HttpServletRequest request){
        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        Integer id=(Integer)request.getSession().getAttribute("userId");
        BasUser user=userService.selectByKey(id);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("forget_patternlock_sms");
        Map map = smsInfo(validPhoneCode,user.getMobile(),noticeType);
        map.put("mobile",user.getMobile());
        return map;
    }


    //自动投标验证短信
    @ResponseBody
    @RequestMapping(value="/user/autoSms")
    public Object autoSms(HttpServletRequest request){
        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        Integer id=(Integer)request.getSession().getAttribute("userId");
        BasUser user=userService.selectByKey(id);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("auto_sms");
        return smsInfo(validPhoneCode,user.getMobile(),noticeType);
    }

    //修改密码短信
    @ResponseBody
    @RequestMapping(value="/user/modifyPasswordSms")
    public Object modifyPasswordSms(HttpServletRequest request){
        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        Integer id=(Integer)request.getSession().getAttribute("userId");
        BasUser user=userService.selectByKey(id);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("modifypass_sms");
        return smsInfo(validPhoneCode,user.getMobile(),noticeType);
    }

    //第三方绑定登录短信
    @ResponseBody
    @RequestMapping(value="/user/loginBandingSms")
    public Object loginBandingSms(HttpServletRequest request){
        Date newDate=new Date();
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("vaildPhoneCode",validPhoneCode);
        String mobile=request.getParameter("mobile");
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("login_banding_sms");
        return smsInfo(validPhoneCode,mobile,noticeType);

    }
    //绑卡
    @ResponseBody
    @RequestMapping(value="/doTieOnCardSendSms")
    public Object doTieOnCardSendSms(HttpServletRequest request){
        Date newDate=new Date();
        String pictureCode=request.getParameter("pictureCode");
        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        String mobile=request.getParameter("mobile");
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("tie_on_card_sms");
        return smsInfo(validPhoneCode,mobile,noticeType);
    }

    //修改交易密码sms
    @ResponseBody
    @RequestMapping(value="/doModifyPayPasswordSms")
    public Object doModifyPayPasswordSms(HttpServletRequest request){
        Date newDate=new Date();
//        String pictureCode=request.getParameter("pictureCode");
//        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
//        //校验图片验证码
//        if(!verifyCode.equals(pictureCode)) {
//            throw new CrowdException("picture_code_error");
//        }
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        Integer id=(Integer)request.getSession().getAttribute("userId");
        BasUser user=userService.selectByKey(id);
        String mobile=user.getMobile();
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("modify_pay_password");
        return smsInfo(validPhoneCode,mobile,noticeType);
    }

    /**
     * 借款短信
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/loanSms")
    public Object loanSms(HttpServletRequest request){
        Date newDate=new Date();
        String pictureCode=request.getParameter("pictureCode");
        String verifyCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }
        request.getSession().setAttribute("validPhoneExpiredTime",newDate.getTime());
        String validPhoneCode=RandomCodesUtils.createRandom(true, 4);
        request.getSession().setAttribute("validPhoneCode",validPhoneCode);
        Integer id=(Integer)request.getSession().getAttribute("userId");
        BasUser user=userService.selectByKey(id);
        String mobile=user.getMobile();
        SysTempcode tempcode=new SysTempcode();
        tempcode.setAddtime(new Date());
        tempcode.setAddip(IpUtils.get());
        tempcode.setCode(validPhoneCode);
        tempcode.setMobile(mobile);
        tempcodeService.save(tempcode);
        SysNoticeType noticeType=noticeTypeService.getNoticeTypeByNid("loan_sms");
        return smsInfo(validPhoneCode,mobile,noticeType);
    }
}

